#!/usr/bin/env bash

if [[ 0 -eq `echo $0 | grep -c '^/'` ]]; then
    # relative path
    EXEC_PATH=$(dirname "`pwd`/$0")
else
    # absolute path
    EXEC_PATH=$(dirname "$0")
fi

cd $EXEC_PATH || exit 1

# include path
perlcode='if($_=~/(?<=HOME \+ '\'')(.*)(?='\'',)/){print("$1\n")}'
for i in $(cat $(dirname `pwd`)/.ycm_extra_conf.py | perl -ane "$perlcode"); do
    include_params=${include_params}" -I${HOME}$i"
done

# dylib[.so] path #
for j in $(find $(dirname `pwd`) -name "*.so" | grep -v '____build____'); do
    lib_params=${lib_params}" $j"
    LD_LIBRARY_PATH=$(dirname $j):$LD_LIBRARY_PATH
done

export LD_LIBRARY_PATH

for unit in $(find $(dirname `pwd`)/src -name "*_test.sh"); do
    cd `dirname ${unit}`
    printf "\x1b[31;01m[*] ${unit}\x1b[00m\n"
    $unit "$include_params" "$lib_params"
done
